package com.remote.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.result.PageResult;
import com.smartcar.common.result.Result;
import com.remote.common.BlackList;
import com.remote.server.service.BlackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户黑名单
 */

@RestController
@RequestMapping("/black")
public class BlackController {

    @Autowired
    private BlackService blackService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 黑名单列表
     * @param blackList
     * @return
     */

    @PostMapping("/blackList")
    public Result<PageResult<User>> BlackList(@RequestBody BlackList blackList){
        PageHelper.startPage(blackList.getPageNum(),blackList.getPageSize());
        List<User> blackLists = blackService.BlackList();
        PageInfo<User> blackListPageInfo = new PageInfo<>(blackLists);
        return PageResult.toResult(blackListPageInfo.getTotal(),blackLists);
    }

    /**
     *封号
     * @param blackList
     * @return
     */
    @PostMapping("/blocked")
    public void blocked(@RequestBody BlackList blackList){
        blackService.blocked(blackList);
    }

    /**
     * 解封
     * @param phone
     * @return
     */

    @PostMapping("/deblocking")
    public void deblocking(@PathVariable("phone") String phone){
        blackService.deblocking(phone);
    }

    @PostMapping("/selectUser")
    public Result<PageResult<User>> selectUser(){
        List<User> users = blackService.selectUser();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return PageResult.toResult(pageInfo.getTotal(),users);
    }

    @PostMapping("/updateStatus/{phone}")
    public Result<User> updateStatus(@PathVariable("phone") String phone,@PathVariable("endTime") String endTime){
        blackService.updateStatus(phone,endTime);
        return Result.success();
    }

    @RequestMapping("/start")
    public Result start(){
        List<String> user = redisTemplate.opsForList().range("user", 0, -1);
        System.out.println("-=------------------------------------------------");
        for (String s : user) {
            JSONObject jsonObject = JSONObject.parseObject(s);
            System.out.println(jsonObject);
        }
        return Result.success();
    }

    @PostMapping("/quxiao")
    public Result quxiao(){
        List<String> order = redisTemplate.opsForList().range("order", 0, -1);


        List<OrderRequest> list = new ArrayList<>();
        for (String s : order) {

            OrderRequest orderRequest = JSONObject.parseObject(s, OrderRequest.class);
            list.add(orderRequest);
        }
        return Result.success(list);
    }
}
